<template>
    <div class="register-container">
        <h1>注册</h1>
      <el-form ref="registerForm" :model="registerForm" :rules="rules" label-width="100px">
        <el-form-item label="用&nbsp&nbsp&nbsp户&nbsp&nbsp名" prop="username">
          <el-input v-model="registerForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp码" prop="password">
          <el-input type="password" v-model="registerForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('registerForm')">注册</el-button>
          <el-button @click="resetForm('registerForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>

  <script>
  export default {
    data() {
      return {
        registerForm: {
          username: '',
          password: '',
          confirmPassword: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, message: '请再次输入密码', trigger: 'blur' },
            { validator: this.validatePassword, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      validatePassword(rule, value, callback) {
        if (value !== this.registerForm.password) {
          callback(new Error('两次输入密码不一致'));
        } else {
          callback();
        }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('注册成功!');
            this.$router.push('/index');
          } else {
            console.log('表单验证失败!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  };
  </script>

  <style scoped>
  .register-container {
    width: 400px;
    margin: 100px auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 10px;
    background: #efefef;
  }

  .el-form-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  justify-content: center;
}

.el-form-item__label {
  width: 80px; /* 固定宽度，根据需要调整 */
  text-align: right; /* 右对齐 */
  margin-right: 20px; /* 标签和输入框之间的间距 */
  color: #333; /* 字体颜色 */
  font-weight: bold; /* 字体加粗 */
}

.el-input {
  flex-grow: 1; /* 输入框占据剩余空间 */
}

h1{
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 15px;
}

.el-button{
    margin-left: 25px;
}
  </style>
